#include "../HPictureViewPanel.h"
#include "../../include/StdAfx.h"
#include "../../include/HGlobalVariable.h"
HPictureViewPanel::HPictureViewPanel(QWidget *parent)
    :HMoveableWidget(parent)
{
    this->setObjectName("pictureViewPanel");
	this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::Window);
	setAttribute(Qt::WA_TranslucentBackground);
	setWindowModality(Qt::ApplicationModal);

	QFile file(g_strAppPath + QSSRELATIONPATH + "HDomeControlPanelWarningView.qss");
	if(file.open(QIODevice::ReadOnly))
	{
		QString strStyle = file.readAll();
		this->setStyleSheet(strStyle.arg(g_strAppPath + g_strImageRelationPath));
		file.close();
	}

	initUI();
	//this->setStyleSheet("QWidget#pictureViewPanel{background-color:#00000000}");
}

HPictureViewPanel::~HPictureViewPanel()
{

}

void HPictureViewPanel::setImageUrl(QString strUrl)
{
    m_strImageUrl = strUrl;
    initImagesInfo(m_strImageUrl);
}

void HPictureViewPanel::paintEvent(QPaintEvent *event)
{
	QSize panelSize = this->size();
	QImage imageToDraw(m_strImageUrl + "/" + m_strCurrentImageName);
	QSize imageSize = imageToDraw.size();

	QSize imageSizeToDraw;
	int nImageToDrawWidth = -1;
	int nImageToDrawHeight = -1;

	float t_fPanelRatio = (panelSize.width() * 1.0) / (panelSize.height() * 1.0);
	float t_fImageRatio = (imageSize.width() * 1.0) / (imageSize.height() * 1.0);

	if(t_fPanelRatio >= t_fImageRatio)
	{
		if(panelSize.height() >= imageSize.height())
		{
			imageSizeToDraw = imageSize;
		}
		else
		{
			nImageToDrawHeight = panelSize.height();
			nImageToDrawWidth = t_fImageRatio * nImageToDrawHeight;
			imageSizeToDraw.setHeight(nImageToDrawHeight);
			imageSizeToDraw.setWidth(nImageToDrawWidth);
		}
	}
	else
	{
		if(panelSize.width() >= imageSize.width())
		{
			imageSizeToDraw = imageSize;
		}
		else
		{
			nImageToDrawWidth = panelSize.width();
			nImageToDrawHeight = nImageToDrawWidth / t_fImageRatio;
			imageSizeToDraw.setHeight(nImageToDrawHeight);
			imageSizeToDraw.setWidth(nImageToDrawWidth);
		}
	}

	int nX = (panelSize.width() - imageSizeToDraw.width()) / 2;
	int nY = (panelSize.height() - imageSizeToDraw.height()) / 2;

	QRect rectImageToDraw(nX,nY,imageSizeToDraw.width(),imageSizeToDraw.height());

	QPainter painter(this);
	painter.drawImage(this->rect(),QImage(g_strAppPath + g_strImageRelationPath + "HIDI_VIEWWARNIING_BG.png" ));
	//painter.fillRect(rectImageToDraw,Qt::red);
	painter.drawImage(rectImageToDraw,imageToDraw);
	
	QStyleOption opt;
	opt.init(this);
	QPainter p(this);
	style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    QWidget::paintEvent(event);
}

void HPictureViewPanel::initUI()
{
    m_ppbClose = new QPushButton();
    m_ppbClose->setObjectName("lgoin_btn_close");
    m_ppbClose->setFixedSize(NLITTLEICONSIZE,NLITTLEICONSIZE);

    m_ppbPre = new QPushButton();
    m_ppbPre->setObjectName("picView_btn_pre");
    m_ppbPre->setFixedSize(NCONTROLLOGINHEIGHT,NCONTROLLOGINHEIGHT);

    m_ppbNext = new QPushButton();
    m_ppbNext->setObjectName("picView_btn_next");
    m_ppbNext->setFixedSize(NCONTROLLOGINHEIGHT,NCONTROLLOGINHEIGHT);

    m_plblInfo = new QLabel();
    m_plblInfo->setObjectName("picView_lbl_info");
	m_plblInfo->setMinimumHeight(45);
	m_plblInfo->setAlignment(Qt::AlignCenter);

    m_phblUp = new QHBoxLayout();
    m_phblUp->setContentsMargins(0,27,14,0);
    m_phblUp->addStretch(100);
    m_phblUp->addWidget(m_ppbClose,1);

    m_phblMid = new QHBoxLayout();
    m_phblMid->setContentsMargins(25,0,25,0);
    m_phblMid->addWidget(m_ppbPre,1,Qt::AlignVCenter);
    m_phblMid->addStretch(100);
    m_phblMid->addWidget(m_ppbNext,1,Qt::AlignVCenter);


    m_pvblMain = new QVBoxLayout();
    m_pvblMain->setContentsMargins(25,10,25,36);
    m_pvblMain->addLayout(m_phblUp,1);
    m_pvblMain->addLayout(m_phblMid,100);
    m_pvblMain->addWidget(m_plblInfo,1,Qt::AlignHCenter | Qt::AlignBottom);

    this->setLayout(m_pvblMain);

    connect(m_ppbClose,SIGNAL(clicked()),this,SLOT(sltCloseBtnClicked()));
    connect(m_ppbPre,SIGNAL(clicked()),this,SLOT(lstPreBtnClicked()));
    connect(m_ppbNext,SIGNAL(clicked()),this,SLOT(lstNextBtnClicked()));
}

void HPictureViewPanel::loadImage(QString strImageName)
{
    QFileInfo fileInfo(m_strImageUrl + "/" + strImageName);
    if(fileInfo.exists())
    {
        m_strCurrentImageName = strImageName;
        checkBtnState();
        QString strFileName = fileInfo.fileName();

        QStringList strLstFileName = strFileName.split("_");
        if(strLstFileName.count() > 1)
        {
            QString strInfo = strLstFileName[0] + " " + strLstFileName[1];
            m_plblInfo->setText(strInfo);
        }
    }
}

void HPictureViewPanel::checkBtnState()
{
    int nIndex = m_lstImageNames.indexOf(m_strCurrentImageName);
    if(nIndex == 0)
    {
		m_ppbPre->show();
		m_ppbPre->setEnabled(true);
		m_ppbNext->hide();
		m_ppbNext->setEnabled(false);
    }
    else if(nIndex == m_lstImageNames.count() - 1)
    {
		m_ppbPre->hide();
		m_ppbPre->setEnabled(false);
		
		m_ppbNext->show();
		m_ppbNext->setEnabled(true);
    }
    else
    {
		m_ppbPre->show();
        m_ppbPre->setEnabled(true);
		m_ppbNext->show();
        m_ppbNext->setEnabled(true);
    }
}

void HPictureViewPanel::initImagesInfo(QString strImageUrl)
{
    QDir dir(strImageUrl);
    m_lstImageNames = dir.entryList(QDir::NoDotAndDotDot | QDir::Files,QDir::Name);

	if(m_lstImageNames.count() > 0)
	{
		loadImage(m_lstImageNames.last());
		update();
	}
}

void HPictureViewPanel::lstPreBtnClicked()
{
	int nIndexCurrent = m_lstImageNames.indexOf(m_strCurrentImageName);
	if(nIndexCurrent < m_lstImageNames.count() - 1)
	{
		QString strNextImage = m_lstImageNames.at(nIndexCurrent + 1);
		loadImage(strNextImage);
	}
	this->repaint();
}

void HPictureViewPanel::lstNextBtnClicked()
{
	int nIndexCurrent = m_lstImageNames.indexOf(m_strCurrentImageName);
	if(nIndexCurrent > 0)
	{
		QString strPreImage = m_lstImageNames.at(nIndexCurrent - 1);
		loadImage(strPreImage);
	}
	this->repaint();
}

void HPictureViewPanel::sltCloseBtnClicked()
{
    m_lstImageNames.clear();
    m_strCurrentImageName = "";
    this->hide();

    emit sigPictureViewPanelClosed();
}
